package dev.ece.suit.hdr;

import java.io.InputStream;

import org.lwjgl.opengl.GL20;

import dev.ece.core.shader.Shader;
import dev.ece.core.texture.Texture;

public class HDRShader extends Shader {

	private int enableHandler;
	
	private int textureHandler;
	
	private int gammaHandler;
	
	private int exposureHandler;
	
	public HDRShader(InputStream vertexShaderInputStream,
			InputStream pixelShaderInputStream) {
		super(vertexShaderInputStream, pixelShaderInputStream);
	}
	
	@Override
	protected void onInitializeHandler(int program) {
		enableHandler = GL20.glGetUniformLocation(program, "enable");
		textureHandler = GL20.glGetUniformLocation(program, "texture");
		gammaHandler = GL20.glGetUniformLocation(program, "gamma");
		exposureHandler = GL20.glGetUniformLocation(program, "exposure");
	}

	@Override
	protected void onBindAttribLocations(int program) {
		GL20.glBindAttribLocation(program, 0, "vao_Vertex");
		GL20.glBindAttribLocation(program, 1, "vao_TexCoord");
	}
	
	public void glEnable(boolean enable) {
		GL20.glUniform1i(enableHandler, enable ? 1 : 0);
	}
	
	public void glGamma(float gamma) {
		GL20.glUniform1f(gammaHandler, gamma);
	}
	
	public void glTexture(Texture texture) {
		GL20.glUniform1i(textureHandler, texture.bind(0));
	}
	
	public void glExposure(float exposure) {
		GL20.glUniform1f(exposureHandler, exposure);
	}

	public static HDRShader createHDRShader() {
		HDRShader shader = new HDRShader(
				HDRShader.class.getClassLoader().getResourceAsStream("shaders/hdr.vert"),
				HDRShader.class.getClassLoader().getResourceAsStream("shaders/hdr.frag"));
		return shader;
	}

	@Override
	public void clearup() {
		GL20.glDeleteShader(this.enableHandler);
		GL20.glDeleteShader(this.textureHandler);
		GL20.glDeleteShader(this.gammaHandler);
		GL20.glDeleteShader(this.exposureHandler);
		super.clearup();
	}
	
}
